<?php
/**
 * 工地监工选择
 * User: Administrator
 * Date: 2019/4/19/019
 * Time: 16:02
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\OrderInfo;
use app\Lib\Model\DbModel\OrderReviewSupervisor;
use app\Lib\Model\User\User;

class OrderSupervisor
{
    private $user;
    private $order;
    private $db;
    private $reason;
    /** @var User $supervisorUser */
    private $supervisorUser;
    private $reviewId;

    public function __construct(User $user ,Order $order)
    {
        $this->user =  $user;
        $this->order = $order;
        $this->db = new OrderReviewSupervisor();
    }

    public function setSupervisorUser( User $user){
        $this->supervisorUser = $user;
    }

    public function setReason($reason){
        $this->reason = $reason;
    }

    public function setReview($reviewId){
        $this->reviewId = $reviewId;
    }

    //监理什么时候都能更换

    /**
     * 选择监理
     * @return bool
     * @throws BlException
     */
    public function selectSupervisor(){
        $order  = $this->order;
        if(!$this->supervisorUser instanceof  User){
            throw new BlException('获取监理失败');
        }

        $orderId = $order->get_order_id();
        $table = new OrderInfo();
        $dataId = $table
            ->where([$table->_id=>$orderId])
            ->update([$table->_site_supervisor_uid=>$this->supervisorUser->getUid()]);

        if($dataId === false){
            throw new BlException('设置监理失败');
        }
//        $data = [
//            $table->_create_time =>time(),
//            $table->_site_supervisor_uid=>$this->supervisorUser->getUid(),
//            $table->_create_uid=>$this->user->getUid(),
//            $table->_reason=>$this->reason,
//            $table->_order_id=>$orderId
//        ];
//        $result = $table->insert($data);
//        if(!$result){
//            throw new BlException('提交请求失败');
//        }
        $this->order->delAllCache();
        $model = new OrderPush($order);
        $model->agreeModerated();
        return true;
    }

    /**
     * 同意监理  只有设计总监和管理员可以操作
     * @return bool
     * @throws BlException
     */
    public function agree(){

        if(!$this->user->getRoleClass()->isDesignDirector() && !$this->user->getRoleClass()->isSuperAdministrator()){
            return true;
        }

        //修改申请
        if(!$this->reviewId || empty($this->reviewId)){
            throw new BlException('未获取到审核数据');
        }
        $table = $this->db;
        $data = [
            $table->_review_time =>time(),
            $table->_review_uid=>$this->user->getUid(),
            $table->_reason=>$this->reason,
            $table->_review_status=>1,
            $table->_id =>$this->reviewId
        ];

        $result = $table->update($data);
        if(!$result){
            throw new BlException('操作失败');
        }
        $supervisor_uid = $table->where([$table->_id=>$this->reviewId])->value($table->_site_supervisor_uid);

        //修改监理
        if($this->order->get_site_supervisor_uid() == $supervisor_uid){
            return true ;
        }
        $model =  (new OrderInfo());
        $data  = [
            $model->_site_supervisor_uid=>$supervisor_uid,
            $model->_id=>$this->order->get_order_id(),
        ];
        $result = $model->update($data);
        if(!$result){
            throw new BlException('修改监理失败');
        }
        $this->order->delAllCache();
        //推送监理
        $model =  (new OrderPush($this->order));
        $model->agreeModerated();
        return true;
    }

    /**
     * 拒绝更换监理
     * @return bool
     * @throws BlException
     */
    public function refuse(){
        if(!$this->user->getRoleClass()->isDesignDirector() && !$this->user->getRoleClass()->isSuperAdministrator()){
            return true;
        }
        //修改申请
        $table = $this->db;
        $data = [
            $table->_review_time =>time(),
            $table->_review_uid=>$this->user->getUid(),
            $table->_review_status=>-1,
            $table->_id =>$this->reviewId,
            $table->_refuse_reason=>$this->reason,
        ];
        $result = $table->update($data);
        if(!$result){
            throw new BlException('操作失败');
        }
        $order =  $this->order;
        $model = new OrderPush($order);
        $model->refuseModerated();
        return true;

    }
}